﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.Web;
using System.Xml;
using MethodWorx.CMS.TemplateLanguage.Properties;
using MethodWorx.CMS.TemplateLanguage.Design;

namespace MethodWorx.CMS.TemplateLanguage.Parsers
{
    public class ContentImageParser : IParser, IDesignable, IEditable
    {
        [ContentTemplateProperty]
        public string Template { get; set; }
        [ContentTemplatePropertyPropertyAttribute("Template", Alias="Name")]
        public string PropertyName { get; set; }
        [Property]
        public int Index { get; set; }
        [Property]
        public bool Editable { get; set; }
        [Property(Alias="Class")]
        public string CssClass { get; set; }

        public object Parse(CmsRequestContext context, IEnumerable<object> stack, Dictionary<string, object> parameters)
        {
            var page = stack.OfType<MethodWorx.CMS.Content.Page>().LastOrDefault();
            if (page == null)
                throw new Exception("No page on the stack");
            else
            {
                //
                //  ok, find the property
                var contentItem = this.GetContentItem(page);
                if (contentItem != null)
                {
                    PageProperty found = null;
                    if (context.Preview)
                        found = contentItem.Properties.FirstOrDefault(p => p.Name.ToLower() == this.PropertyName.ToLower());
                    else
                        found = contentItem.PublishedProperties.FirstOrDefault(p => p.Name.ToLower() == this.PropertyName.ToString().ToLower());

                    if (found != null && found.Value != null)
                    {
                        return GetNodeValue(found, parameters);
                    }
                }
            }

            return null;
        }

        public string GetNodeValue(PageProperty property, Dictionary<string, object> parameters)
        {
            //var val = tag + property.Value.ToString() + "</tag>";
           /* string val;
            if (string.IsNullOrEmpty(this.Tag))
                val = property.Value.ToString();
            else
                val = "<" + this.Tag + ">" + property.Value.ToString() + "</" + this.Tag + ">";

            try
            {
                XmlDocument document = new XmlDocument();
                document.LoadXml(val);
                foreach (string parameter in parameters.Keys)
                {
                    if (!this.Parameters.Select(p => p.ToLower()).Contains(parameter.ToLower()))
                    {
                        document.FirstChild.Attributes.Append(
                            document.CreateAttribute(parameter.ToLower()));
                        document.FirstChild.Attributes[parameter.ToLower()].Value = parameters[parameter].ToString();
                    }
                }

                return document.OuterXml;
            }
            catch (Exception ex)
            {
                return val;
            }*/
            return "";
        }

        private ContentItem GetContentItem(MethodWorx.CMS.Content.Page page)
        {
            var contentItems = page.ChildContent.Select(c => c);
            if (!string.IsNullOrEmpty(this.Template))
                contentItems = contentItems.Where(c => c.Template.Name.ToLower() == this.Template.ToLower());

            contentItems = contentItems.Skip(this.Index).Take(1);
            return contentItems.FirstOrDefault();
        }

        public string Name
        {
            get { return "Image"; }
        }

        public string Namespace
        {
            get { return "Content"; }
        }

        public void Design(IDesigner designer)
        {
            designer.AddControl("Content:Image")
                .AddContentBeforeChildren("<img src=\"/images/icons/picture.png\" class=\"" + (this.CssClass ?? "") + "\"/>");
        }

        public void Edit(IEditor editor)
        {
            //
            //  ok, setup the editor for for the current page
            //  and the current content
            var contentItem = this.GetContentItem(editor.CurrentPage);
            if (contentItem != null)
            {
                editor.AddEditor(this.PropertyName, contentItem);
            }
        }
    }
}
